#define _CRT_SECURE_NO_WARNINGS

#include "dynamic_array2.h"

void InitArray(dynamic_array* arr, int capacity)
{
	arr->data = (int*)malloc(capacity * sizeof(int));
	if (arr->data == nullptr)
	{
		fprintf(stderr, "memory alloction failed.\n");
		return;
	}

	arr->size = 0;
	arr->capacity = capacity;
}

void InsertArray(dynamic_array* arr, int value)
{
	if (arr->size == arr->capacity)
	{
		int new_capacity = arr->capacity *= 2;
		int* new_data = (int*)malloc(new_capacity * sizeof(int));
		arr->capacity = new_capacity;
		arr->data = new_data;
	}
	
	arr->data[arr->size++] = value;

}

void DeleteIndex(dynamic_array* arr, int index)
{
	if (index < 0 || index > arr->size - 1)
	{
		fprintf(stderr, "index out of range.\n");
		return;
	}
	
	for (int i = index; i < arr->size; i++)
	{
		arr->data[i] = arr->data[i + 1];
	}
	arr->size--;
	
}
void DeleteValue(dynamic_array* arr, int value)
{
	for (int i = 0; i < arr->size; i++)
	{
		if (arr->data[i] == value)
		{
			DeleteIndex(arr, i);
			break;
		}
	}

	return;
}


void Traverse(dynamic_array* arr)
{
	for (int i = 0; i < arr->size; i++)
	{
		printf("%d ", arr->data[i]);
	}
	printf("\n");
}

void ClearArray(dynamic_array* arr)
{
	free(arr->data);
	arr->data = nullptr;
	arr->size = 0;
	arr->capacity = 0;
}